package com.wsg.lightaccounting.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.wsg.lightaccounting.Model.AccountBooks;

/**
 * Created by dell-pc on 2016/11/8.
 */

public class DBHelper extends SQLiteOpenHelper {
    public static final String CREATE_TABLE_EXPENDITURE="create table expenditure(" +
            "id INTEGER PRIMARY KEY," +
            "money REAL," +
            "date INTEGER," +
            "note_type INTEGER," +
            "money_type INTEGER," +
            "pay_type INTEGER," +
            "consumption_type INTEGER," +
            "mark TEXT," +
            "FOREIGN KEY(note_type) REFERENCES account_books(id)," +
            "FOREIGN KEY(money_type) REFERENCES account(id)," +
            "FOREIGN KEY(consumption_type) REFERENCES expenditure_type(id));";
    public static final String CREATE_TABLE_INCOME = "create table income(" +
            "id INTEGER PRIMARY KEY," +
            "money REAL," +
            "date INTEGER," +
            "note_type INTEGER," +
            "money_type INTEGER," +
            "income_type INTEGER," +
            "mark TEXT," +
            "FOREIGN KEY(note_type) REFERENCES account_books(id)," +
            "FOREIGN KEY(money_type) REFERENCES account(id)," +
            "FOREIGN KEY(income_type) REFERENCES income(id));";
    public static final String CREATE_TABLE_ACCOUNT = "create table account(" +
            "id INTEGER PRIMARY KEY," +
            "balance REAL," +
            "type INTEGER," +
            "color INTEGER)";
    public static final String CREATE_TABLE_NETWORK_PAYMENT = "create table network_payment(" +
            "id INTEGER PRIMARY KEY," +
            "name TEXT," +
            "information TEXT," +
            "account_id INTEGER," +
            "FOREIGN KEY(account_id) REFERENCES account(id));";
    public static final String CREATE_TABLE_CASH_CARD = "create table cash_card(" +
            "id INTEGER PRIMARY KEY," +
            "card_id TEXT," +
            "bank TEXT," +
            "name TEXT," +
            "account_id INTEGER," +
            "FOREIGN KEY(account_id) REFERENCES account(id));";
    public static final String CREATE_TABLE_CREDIT_CARD = "create table credit_card(" +
            "id INTEGER PRIMARY KEY," +
            "card_id TEXT," +
            "name TEXT," +
            "bank TEXT," +
            "credit_limit REAL," +
            "account_id INTEGER," +
            "FOREIGN KEY(account_id) REFERENCES account(id));";
    public static final String CREATE_TABLE_CUSTOMIZED = "create table customized(" +
            "id INTEGER PRIMARY KEY," +
            "name TEXT," +
            "account_id INTEGER," +
            "FOREIGN KEY(account_id) REFERENCES account(id));";
    public static final String CREATE_TABLE_EXPENDITURE_TYPE = "create table expenditure_type(" +
            "id INTEGER PRIMARY KEY," +
            "icon INTEGER," +
            "name TEXT," +
            "is_show INTEGER);";
    public static final String CREATE_TABLE_INCOME_TYPE = "create table income_type(" +
            "id INTEGER PRIMARY KEY," +
            "icon INTEGER," +
            "name TEXT," +
            "is_show INTEGER);";
    public static final String CREATE_TABLE_ACCOUNT_BOOKS = "create table account_books(" +
            "id INTEGER PRIMARY KEY," +
            "name TEXT);";
    public static final String CREATE_TABLE_AA = "create table aa(" +
            "id INTEGER PRIMARY KEY," +
            "name TEXT," +
            "money REAL," +
            "is_finished INTEGER," +
            "expenditure_id INTEGER," +
            "FOREIGN KEY(expenditure_id) REFERENCES expenditure(id));";

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_ACCOUNT_BOOKS);
        db.execSQL(CREATE_TABLE_INCOME_TYPE);
        db.execSQL(CREATE_TABLE_EXPENDITURE_TYPE);
        db.execSQL(CREATE_TABLE_ACCOUNT);
        db.execSQL(CREATE_TABLE_CUSTOMIZED);
        db.execSQL(CREATE_TABLE_CASH_CARD);
        db.execSQL(CREATE_TABLE_CREDIT_CARD);
        db.execSQL(CREATE_TABLE_NETWORK_PAYMENT);
        db.execSQL(CREATE_TABLE_INCOME);
        db.execSQL(CREATE_TABLE_EXPENDITURE);
        db.execSQL(CREATE_TABLE_AA);
        ContentValues[] data = new ContentValues[13];
        for (int i = 0; i < data.length; i++) {
            data[i] = new ContentValues();
        }
        data[0].put("id",0L);
        data[0].put("name","一般");
        data[0].put("icon",0);
        data[0].put("is_show",1);
        db.insert(DB.EXPENDITURE_TYPE_DB_NAME_STRING,null,data[0]);

        data[1].put("name","餐饮");
        data[1].put("icon",1);
        data[1].put("is_show",1);
        db.insert(DB.EXPENDITURE_TYPE_DB_NAME_STRING,null,data[1]);

        data[2].put("name","交通");
        data[2].put("icon",2);
        data[2].put("is_show",1);
        db.insert(DB.EXPENDITURE_TYPE_DB_NAME_STRING,null,data[2]);

        data[3].put("name","默认账本");
        data[3].put("id",0L);
        db.insert(DB.ACCOUNT_BOOKS_DB_NAME_STRING,null,data[3]);

        data[4].put("name","旅游账本");
        db.insert(DB.ACCOUNT_BOOKS_DB_NAME_STRING,null,data[4]);

        data[5].put("id",0L);
        data[5].put("name","一般");
        data[5].put("icon",0);
        data[5].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,data[5]);

        data[6].put("name","工资");
        data[6].put("icon",1);
        data[6].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,data[6]);

        data[7].put("name","生活费");
        data[7].put("icon",2);
        data[7].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,data[7]);



        data[8].put("balance",0f);
        data[8].put("id",0L);
        data[8].put("type",0);
        data[8].put("color",0);
        db.insert(DB.ACCOUNT_DB_NAME_STRING,null,data[8]);

        data[9].put("balance",0f);
        data[9].put("id",1L);
        data[9].put("type",1);
        data[9].put("color",1);
        db.insert(DB.ACCOUNT_DB_NAME_STRING,null,data[9]);

        data[10].put("balance",0f);
        data[10].put("id",2L);
        data[10].put("type",2);
        data[10].put("color",2);
        db.insert(DB.ACCOUNT_DB_NAME_STRING,null,data[10]);

        data[11].put("name","支付宝");
        data[11].put("account_id",1L);
        db.insert(DB.NETWORK_PAYMENT_DB_NAME_STRING,null,data[11]);

        data[12].put("name","储蓄卡");
        data[12].put("account_id",2L);
        db.insert(DB.CASH_CARD_DB_NAME_STRING,null,data[12]);

        ContentValues[] icon = new ContentValues[20];
        for (int i = 0; i < 20; i++) {
            icon[i] = new ContentValues();
        }
        icon[0].put("id",3L);
        icon[0].put("name","兼职");
        icon[0].put("icon",3);
        icon[0].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,icon[0]);

        icon[1].put("id",4);
        icon[1].put("name","理财投资");
        icon[1].put("icon",4);
        icon[1].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,icon[1]);
        icon[2].put("id",5);
        icon[2].put("name","报销退款");
        icon[2].put("icon",5);
        icon[2].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,icon[2]);
        icon[3].put("id",6);
        icon[3].put("name","红包");
        icon[3].put("icon",6);
        icon[3].put("is_show",1);
        db.insert(DB.INCOME_TYPE_DB_NAME_STRING,null,icon[3]);
        icon[4].put("id",4);
        icon[4].put("name","零食");
        icon[4].put("icon",4);
        icon[4].put("is_show",1);
        db.insert(DB.EXPENDITURE_TYPE_DB_NAME_STRING,null,icon[4]);
        for (int i = 5; i < 20; i++) {
            icon[i].put("id",i);
            icon[i].put("icon",i);
            icon[i].put("is_show",1);
        }
        icon[5].put("name","购物");
        icon[6].put("name","烟酒");
        icon[7].put("name","水果");
        icon[8].put("name","日用");
        icon[9].put("name","娱乐");
        icon[10].put("name","电影");
        icon[11].put("name","红包");
        icon[12].put("name","话费");
        icon[13].put("name","社交");
        icon[14].put("name","房租房贷");
        icon[15].put("name","火车机票");
        icon[16].put("name","酒店");
        icon[17].put("name","药品");
        icon[18].put("name","服装");
        icon[19].put("name","美容化妆");
        for (int i = 5; i < 20; i++) {
            db.insert(DB.EXPENDITURE_TYPE_DB_NAME_STRING,null,icon[i]);
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

}
